package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.*;
import com.antler.smt.portal.mapper.EducationalExperienceEntityMapper;
import com.antler.smt.portal.mapper.ExperienceEntityMapper;
import com.antler.smt.portal.mapper.ProfessionalSkillEntityMapper;
import com.antler.smt.portal.mapper.ProjectExpertienceEntityMapper;
import com.antler.smt.request.portal.PeopleReq;
import com.antler.smt.service.portal.SavePeopleInfoService;
import com.antler.smt.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


/**
 * 异步插入各种集合实体
 *
 * @author zhangyi
 * @date 2018/9/19 14:09
 */
@Service
@Slf4j
public class SavePeopleInfoServiceImpl implements SavePeopleInfoService {

    @Autowired
    private ExperienceEntityMapper experienceEntityMapper;

    @Autowired
    private EducationalExperienceEntityMapper educationalExperienceEntityMapper;

    @Autowired
    private ProjectExpertienceEntityMapper projectExpertienceEntityMapper;

    @Autowired
    private ProfessionalSkillEntityMapper professionalSkillEntityMapper;

    @Async("asyncSavePeople")
    @Override
    public void savePeopleInfo(PeopleReq req,Long id) {
        log.info("进入异步线程执行逻辑.......");
        //工作经历
        req.getExperience().stream()
                .forEach( e -> {
                    ExperienceEntity experienceEntity = new ExperienceEntity();
                    experienceEntity.setPeopleInfoId(id);
                    experienceEntity.setWorkAttachemtId(e.getWorkAttachemtId());
                    experienceEntity.setWorkCompanyName(e.getWorkCompanyName());
                    experienceEntity.setWorkDescription(e.getWorkDescription());
                    experienceEntity.setWorkEndTime(e.getWorkEndTime());
                    experienceEntity.setWorkName(e.getWorkName());
                    experienceEntity.setWorkStartTime(e.getWorkStartTime());
                    experienceEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                    experienceEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                    experienceEntityMapper.insertSelective(experienceEntity);
                });

        //项目经历
        req.getProjectExpertiences().stream()
                .forEach(e -> {
                    ProjectExpertienceEntity pro = new ProjectExpertienceEntity();
                    pro.setProjectName(e.getProjectName());
                    pro.setDescription(e.getDescription());
                    pro.setPosition(e.getPosition());
                    pro.setStartTime(e.getStartTime());
                    pro.setPeopleInfoId(id);
                    pro.setEndTime(e.getEndTime());
                    pro.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                    pro.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                    projectExpertienceEntityMapper.insertSelective(pro);
                });
        //教育经历
        req.getEducationalExperiences().stream()
                .forEach(e -> {
                    EducationalExperienceEntity edu = new EducationalExperienceEntity();
                    edu.setCollegeName(e.getCollegeName());
                    edu.setAdmission(e.getAdmission());
                    edu.setGraduation(e.getGraduation());
                    edu.setPeopleInfoId(id);
                    edu.setProfession(e.getProfession());
                    edu.setSchoolName(e.getSchoolName());
                    edu.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                    educationalExperienceEntityMapper.insertSelective(edu);
                });
        //专业技能
        req.getProfessionalSkills().stream()
                .forEach(e -> {
                    ProfessionalSkillEntity pro = new ProfessionalSkillEntity();
                    pro.setSkillName(e.getSkillName());
                    pro.setSkillDesc(e.getSkillDesc());
                    pro.setPeopleInfoId(id);

                    pro.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
                    professionalSkillEntityMapper.insertSelective(pro);
                });
    }
}
